package org.study.redis.api.util.jedis;

import org.study.redis.api.constant.RedisProperties;
import redis.clients.jedis.Jedis;

/**
 * Redis 操作工具类：Server
 * 参考 {@link redis.clients.jedis.commands.ServerCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisServerUtils {
    private RedisServerUtils() {
        throw new AssertionError("不能实例化 RedisServerUtils 工具类");
    }

    /**
     * 此命令通常用于测试连接是否仍然存在，或测量延迟
     *
     * @return java.lang.String
     */
    public static String ping() {
        String ping = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            ping = jedis.ping();
        }
        return ping;
    }

    public static String ping(String message) {
        String ping = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            ping = jedis.ping(message);
        }
        return ping;
    }

    /**
     * INFO 命令以计算机易于解析且人类易于阅读的格式返回有关服务器的信息和统计信息
     *
     * @return java.lang.String
     */
    public static String info() {
        String info = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            info = jedis.info();
        }
        return info;
    }

    /**
     * INFO 命令以计算机易于解析且人类易于阅读的格式返回有关服务器的信息和统计信息
     *
     * @param section all: Return all sections,
     *                default: Return only the default set of sections,
     *                server: General information about the Redis server,
     *                clients: Client connections section,
     *                memory: Memory consumption related information,
     *                persistence: RDB and AOF related information,
     *                stats: General statistics,
     *                replication: Master/slave replication information,
     *                cpu: CPU consumption statistics,
     *                commandstats: Redis command statistics,
     *                cluster: Redis Cluster section,
     *                keyspace: Database related statistics
     * @return java.lang.String
     */
    public static String info(String section) {
        String info = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            info = jedis.info(section);
        }
        return info;
    }

    /**
     * 请求在受密码保护的 Redis 服务器中进行身份验证
     *
     * @param password 密码
     * @return java.lang.String
     */
    public static String auth(String password) {
        String auth = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            auth = jedis.auth(password);
        }
        return auth;
    }

    public static String echo(String string) {
        String echo = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            echo = jedis.echo(string);
        }
        return echo;
    }

    /**
     * SAVE 命令执行数据集的同步保存，以 RDB 文件的形式生成 Redis 实例内所有数据的时间点快照
     *
     * @return java.lang.String
     */
    public static String save() {
        String save = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            save = jedis.save();
        }
        return save;
    }

    /**
     * 在后台保存数据库
     *
     * @return java.lang.String
     */
    public static String bgsave() {
        String bgsave = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bgsave = jedis.bgsave();
        }
        return bgsave;
    }

    /**
     * 指示 Redis 启动 Append Only File 重写过程
     *
     * @return java.lang.String
     */
    public static String bgrewriteaof() {
        String bgrewriteaof = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bgrewriteaof = jedis.bgrewriteaof();
        }
        return bgrewriteaof;
    }

    /**
     * 返回最后一次成功执行的数据库保存的 UNIX 时间
     *
     * @return long
     */
    public static long lastsave() {
        long lastsave;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lastsave = jedis.lastsave();
        }
        return lastsave;
    }

    /**
     * 删除当前选中 DB 中的所有 key
     *
     * @return java.lang.String
     */
    public static String flushDB() {
        String flushDB = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            flushDB = jedis.flushDB();
        }
        return flushDB;
    }

    /**
     * 删除所有数据库中的所有键，而不仅仅是当前选择的一个
     *
     * @return java.lang.String
     */
    public static String flushAll() {
        String flushAll = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            flushAll = jedis.flushAll();
        }
        return flushAll;
    }

    /**
     * 停止所有客户端
     */
    public static void shutdown() {
        try (Jedis jedis = SysJedisPool.getResource()) {
            jedis.shutdown();
        }
    }

    public static void main(String[] args) {
        System.out.println("ping：" + ping());
        System.out.println("ping：" + ping("String message"));
        System.out.println("auth：" + auth(RedisProperties.PASSWORD));

        System.out.println("info：" + info());
        System.out.println("info：" + info("cpu"));

        System.out.println("echo：" + echo("String message"));

        System.out.println("save：" + save());
        System.out.println("bgsave：" + bgsave());
        System.out.println("lastsave：" + lastsave());

        // System.out.println("flushDB：" + flushDB());
        // System.out.println("flushAll：" + flushAll());
    }
}
